package service;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import data.Specialty;

import entity.Doctor;

public class DoctorService 
{

	// ********************************************************************************************************** start
    // ** public methods ***	
	
	/**
	 * @throws IOException
	 */
	public void fillInfo() 
		throws 
			IOException 
	{

		FileOutputStream fileOutput = new FileOutputStream("src/storage/doctors.txt");
		ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
		ArrayList<Doctor> doctors = new ArrayList<Doctor>();

		Doctor cardiology = new Doctor();
		cardiology.setId(1);
		cardiology.setName("Belkina");
		cardiology.setNumberRoom(56);
		cardiology.setSpecialty(Specialty.CARDIOLOGY);
		doctors.add(cardiology);

		Doctor gastro = new Doctor();
		gastro.setId(2);
		gastro.setName("Kuznetcov");
		gastro.setNumberRoom(34);
		gastro.setSpecialty(Specialty.GASTROENTEROLOGY);
		doctors.add(gastro);

		Doctor nevrology = new Doctor();
		nevrology.setId(3);
		nevrology.setName("Kozlova");
		nevrology.setNumberRoom(122);
		nevrology.setSpecialty(Specialty.NEUROLOGY);
		doctors.add(nevrology);

		Doctor urology = new Doctor();
		urology.setId(4);
		urology.setName("Ivanova");
		urology.setNumberRoom(23);
		urology.setSpecialty(Specialty.UROLOGY);
		doctors.add(urology);

		Doctor endo = new Doctor();
		endo.setId(5);
		endo.setName("Petrov");
		endo.setNumberRoom(752);
		endo.setSpecialty(Specialty.ENDOCRINOLOGY);
		doctors.add(endo);

		//write list of doctors
		objectOutput.writeObject(doctors);
		objectOutput.flush();
		objectOutput.close();
		
	}
	
	/**
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Doctor> readInfo() 
			throws 
				IOException, 
				ClassNotFoundException 
	{
		
		ArrayList<Doctor> doctors = new ArrayList<Doctor>();
		FileInputStream fileInput = new FileInputStream("src/storage/doctors.txt");
		ObjectInputStream objectInput = new ObjectInputStream(fileInput);
		
		//read list of doctors
		doctors =  (ArrayList<Doctor>) objectInput.readObject();

		//print list of doctors
		for (Doctor doctor : doctors)
		{
			System.out.println(doctor.getId() + ",  "+ doctor.getSpecialty().getTitle() + ",  name: " +
					doctor.getName() + ",  number of room:" + doctor.getNumberRoom());
		}
		
		fileInput.close();
		objectInput.close();
		
		return doctors;
			
	}
	
	/**
	 * @param id
	 * @param doctors
	 * @return doctor
	 */
	public Doctor findDoctor(
			int id, 
			ArrayList<Doctor> doctors) 
	{

		for (Doctor doctor: doctors)
		{
			if (doctor.getId() == id)
			{			
				return doctor;
			}
		}
		
		return null;
			
	}
	
	// ********************************************************************************************************** end
	
}
